//
//  TYBaseTabBarController.m
//  Class
//
//  Created by wbb on 2020/3/3.
//  Copyright © 2020 TY. All rights reserved.
//

#import "TYBaseTabBarController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "MessageViewController.h"
#import "TYBaseNavigationController.h"

@interface TYBaseTabBarController ()

/** 当前索引 */
@property (nonatomic, assign) NSInteger currentIndex;
/** TabBar上的ImageViewArray */
@property (nonatomic, copy) NSArray *tabBarImageVArray;

@end


@implementation TYBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
    
    [self addTabBarVC:[[HomeViewController alloc] init] withTitle:@"首页" withNormalImage:@"course_tabbar_normal" withSelectImage:@"course_tabbar_select"];
    [self addTabBarVC:[[MessageViewController alloc] init] withTitle:@"消息" withNormalImage:@"message_tabbar_normal" withSelectImage:@"message_tabbar_select"];

    [self addTabBarVC:[[MineViewController alloc] init] withTitle:@"我的" withNormalImage:@"mine_tabbar_normal" withSelectImage:@"mine_tabbar_select"];
}

- (void)addTabBarVC:(UIViewController *)viewController withTitle:(NSString *)title withNormalImage:(NSString *)normalImage withSelectImage:(NSString *)selectImage {

    if (@available(iOS 13.0, *)) {
        UITabBar *tabBar = [UITabBar appearance];
        [tabBar setTintColor:hexColor(2481ff)];
        [tabBar setUnselectedItemTintColor:[UIColor lightGrayColor]];
    }
    else {
        UITabBarItem *tabBarItem = [UITabBarItem appearance];
        [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} forState:UIControlStateNormal];
        [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: hexColor(2481ff)}forState:UIControlStateSelected];
    }
    
    
    viewController.tabBarItem.title = title;

    viewController.tabBarItem.image = [UIImage imageNamed:normalImage];
    viewController.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
    TYBaseNavigationController *nav = [[TYBaseNavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:nav];
}



/** 点击tabbarItem自动调用 */
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index != _currentIndex) {
        // 不需要重复添加
        if (self.tabBarImageVArray.count == 0) {
            self.tabBarImageVArray = [self throughTabBarSubviewsMakeUpImageArray];
        }
        [self animationWithIndex:index];
        _currentIndex = index;
    }
}

/** 遍历，将TabBar上需要动画的控件集合成数组 */
- (NSArray *)throughTabBarSubviewsMakeUpImageArray {
    
    NSMutableArray *imageMuArr = [NSMutableArray arrayWithCapacity:0];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            /** 这一层for循环分离UITabBarButton中的imageV和label两个Subview，目的是只让图片有动画效果，或者只让文字有动画效果 */
            // 当前只让图片有动画效果
            for (UIImageView *barImageV in tabBarButton.subviews) {
                if ([barImageV isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                    [imageMuArr addObject:barImageV];
                }
            }
        }
    }
    return [imageMuArr mutableCopy];
}

/**
 * 图片动画
 
 * CABasicAnimation类的使用方式就是基本的关键帧动画。
 * 所谓关键帧动画，就是将Layer的属性作为KeyPath来注册，指定动画的起始帧和结束帧，然后自动计算和实现中间的过渡动画的一种动画方式。
 */
- (void)animationWithIndex:(NSInteger)index {
    
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.1;
    pulse.repeatCount = 1;
    pulse.autoreverses = YES;
    pulse.fromValue = [NSNumber numberWithFloat:0.9];
    pulse.toValue = [NSNumber numberWithFloat:1.1];
    UIImageView *animalImageV = self.tabBarImageVArray[index];
    [animalImageV.layer addAnimation:pulse forKey:nil];
}

- (NSArray *)tabBarImageVArray {
    if (!_tabBarImageVArray) {
        _tabBarImageVArray = [NSArray array];
    }
    return _tabBarImageVArray;
}

@end
